package com.base.commons.elasticsearch.utils;

import com.base.commons.tools.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 * @Author zt17611
 * @Description //TODO
 * @Date 2021/3/16/016 14:29
 * @Version 0.1
 **/
public class ElasticsearchIndexUtil {


    /**
     * 2020-12-31 16:00:00 是 UTC时间，北京时间是2021-01-01
     */
//    private static Date startDate = DateUtils.stringToDate("2020-12-31 16:00:00", "yyyy-MM-dd HH:mm:ss");

    /**
     *
     [index, beginDate, endDate]
     * @Author zt17611
     * @Description 根据时间计算索引
     * @Date 2021/3/16/016 15:00
     * @return java.lang.String[]
     **/
    public static String[] generateElasticsearchIndex(String index, Date beginDate, Date endDate) {
        /*if (beginDate == null || endDate == null) {
            throw new NullPointerException("beginDate and endDate must not null");
        }*/
        //索引值为空，直接返回空数据
        if(StringUtils.isEmpty(index)){
            return null;
        }

        //开始时间或结束时间为空，直接返回index_*
        if(Objects.isNull(beginDate) || Objects.isNull(endDate)){
            String[] indexs = new String[1];
            indexs[0]=index+"-*";
            return indexs;
        }

        //结束时间小于2021年，直接返回空数据
//        if(endDate.before(startDate)){
//            return null;
//        }

        //结束时间大于当前时间，设置为当前时间
        Date nowDate =  new Date();
        if(endDate.after(nowDate)){
            endDate = nowDate;
        }

        //开始时间小于2021年，将开始时间转成2021年1月1日0点0分0秒
//        if (beginDate.before(startDate)) {
//            beginDate = startDate;
//        }

        //开始时间等于结束时间
        if(Objects.equals(beginDate,endDate)){
            String[] indexs = new String[1];
            indexs[0]=index+"-"+DateUtils.format(beginDate,"yyyy-MM");
            return indexs;
        }

        //根据时间计算ES索引
        List<Date> dates = getDates(beginDate,endDate);
        if(CollectionUtils.isEmpty(dates)){
           return null;
        }

        List<String> dateStrs = new ArrayList<>();
        dates.stream().forEach(item->{
            String dateStr = DateUtils.format(item,"yyyy-MM");
            if(!dateStrs.contains(dateStr)){
                dateStrs.add(dateStr);
            }
        });

        String[] indexs = new String[dateStrs.size()];
        for(int i = 0;i<dateStrs.size();i++){
            indexs[i] = index + "-" + dateStrs.get(i);
        }
        return indexs;
    }

    /**
     *
     [beginDate, endDate]
     * @Author zt17611
     * @Description 获取2个日期间的所有日期
     * @Date 2021/3/16/016 14:49
     * @return java.util.List<java.util.Date>
     **/
    public static List<Date> getDates(Date beginDate, Date endDate){

        List<Date> result = new ArrayList<Date>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(beginDate);
        tempStart.add(Calendar.DAY_OF_YEAR, 0);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(endDate);
        while (tempStart.before(tempEnd)) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    public static void main(String[] args) {
        String index = null;
        String[] indexs = generateElasticsearchIndex("",null,null);
        System.out.println(indexs);
        index = "fbg_fps_profit_margin_transit_detail";
        indexs = generateElasticsearchIndex(index,null,null);
        if(Objects.nonNull(indexs)){
            for(int i=0;i<indexs.length;i++){
                System.out.println(indexs[i]);
            }
        }
        Date beginDate = DateUtils.stringToDate("2020-01-02","yyyy-MM-dd");
        Date end = DateUtils.stringToDate("2022-01-02","yyyy-MM-dd");
        indexs = generateElasticsearchIndex(index,beginDate,end);
        if(Objects.nonNull(indexs)){
            for(int i=0;i<indexs.length;i++){
                System.out.println(indexs[i]);
            }
        }

    }
}
